﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../../common/jsp/taglibs.jsp"%>
<!-- 
    Copyright (C)  2011  chen wei hua.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 2.0
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU Free Documentation License".
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>采购管理-新建采购单</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<%@ include file="../../common/jsp/config.jsp"%>
<%@ include file="../../common/jsp/validate.jsp"%>
<%@ include file="../../common/jsp/autocomplete.jsp"%>
<script type="text/javascript" src="common/js/calendar/WdatePicker.js" ></script>
<script type="text/javascript" src="common/js/jquery/chinaArea/jquery.fillOptions.js"></script>
<script type="text/javascript">
var rowIndex = 0;
function addLine() {
	rowIndex++;
	var table = $("#materialsList");
	table.find("tr").last().before(
	           '<tr class="odd">'
	           +'<td><input type="button" value="请选择" onclick="openSelectDialog('+rowIndex+')" />'
	           +'<s:hidden id="materialsNos-'+rowIndex+'" name="materialsNos" /></td>'
	           +'<td><span id="materialsRootCategorys-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsCategorys-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsNames-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsTypes-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsSkuNameSpans-'+rowIndex+'"></span>'
	           +'<s:hidden id="materialsIds-'+rowIndex+'" name="materialsIds" />'
	           +'<s:hidden id="materialsSkuNames-'+rowIndex+'" name="materialsSkuNames" /></td>'
	           +'<td><span id="materialsUnits-'+rowIndex+'"></span></td>'
	           +'<td><span id="storeNumbers-'+rowIndex+'"></span></td>'
	           +'<td><s:textfield id="planNos-'+rowIndex+'" name="planNos" size="10" cssClass="{maxlength:10}" /></td>'
	           +'<td><s:textfield id="materialsNums-'+rowIndex+'" name="materialsNums" size="5" cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount('+rowIndex+')"/></td>'
	           +'<td><s:textfield id="materialsPrices-'+rowIndex+'" name="materialsPrices" size="5" cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount('+rowIndex+')"/></td>'
	           +'<td><s:textfield id="materialsAmounts-'+rowIndex+'" name="materialsAmounts" readonly="true" size="10" value="0" cssClass="inputdisabled"/></td>'
	           +'<td><s:textfield id="remarks-'+rowIndex+'" name="remarks" size="15" cssClass="{maxlength:30}" /></td>'
	           +'<td><input type="button" value="删除" class="input1" onclick="deleteRow(this)" /></td>'
	           +'</tr>');
}

function computerTotleAmount() {
	var totleNumber = 0;
	for (var i = 0; i < rowIndex + 1; i++) {
		var amount = $("#materialsAmounts-" + i);
		if (amount.length > 0) {
			totleNumber += parseFloat(amount.val());
		}
	}
	$("#order_totleAmount").val(totleNumber.toFixed(2));
}

function computerAmount(id) {
	var number = $("#materialsNums-" + id).val();
	var prices = $("#materialsPrices-" + id).val();
	var totleValue = number * prices;
	$("#materialsAmounts-" + id).val(totleValue.toFixed(2));
	computerTotleAmount();
}

function deleteRow(button) {
	$(button).parents("tr").remove();
	computerTotleAmount();
}

function openSelectDialog(id) {
 	alertDialog(700, 500, "showSelectMaterialsDialog.action", 
 	function (result) {
 		var materials = result.materials;
 		if (materials) {
 			var fieldNames = "";
 			var skuId = result.skuId;
 		   	var skuConfig = materials.skuDetails;
 		   	var storeNumber = 0;
 	 	   	for (var i in skuConfig) {
 	 	 	   	var config = skuConfig[i];
 	 	 	   	if (config.id == skuId) {
 	 	 	   		storeNumber = config.number;
 	 	 	 	 	for (var j in config.fields) {
 	  	   				var field = config.fields[j];
 	  	   				fieldNames += field.customName + " ";
 	 	 	 	 	}
 	 	 	 	 	break;
 	 	 	   	}
 	 	   	}

 	 	    $("#materialsNos-" + id).val(materials.code);
 	 	    $("#materialsRootCategorys-" + id).html(materials.rootCategory.name);
 	 	    $("#materialsCategorys-" + id).html(materials.category.name);
 	 	    $("#materialsNames-" + id).html(materials.name);
 	 	    $("#materialsTypes-" + id).html(materials.type);
 	 	    $("#materialsSkuNameSpans-" + id).html(fieldNames);
 	 	    $("#materialsIds-" + id).val(skuId);
 			$("#materialsSkuNames-" + id).val(fieldNames);
 			$("#materialsUnits-" + id).html(materials.units);
 			$("#storeNumbers-" + id).html(storeNumber);
 		}
 	});
}

$(document).ready(function() {
	$("#saveSupplierOrder").validate();
	$("#order_supplierName").autocomplete("querySupplierNames.action", {
		width: 300,
		max: 10,
		autoFill: false,
		dataType: "json",
		parse: function(data) {
			var parsed = [];
			var rows = data.supplierList;
			for (var i=0; i < rows.length; i++) {
				var row = rows[i];
				if (row) {
					parsed[parsed.length] = {
						data: row,
						value: row[0],
						result: row[1]//最终选择后显示的内容
					};
				}
			}
			return parsed;
		},
		formatItem: function(row, i, max) {
			return row[1];//列表中显示的值
		}
	}).result(function(e, row) {
		$("#order_supplierId").val(row[0]);
	});
});
</script>
</HEAD>
<BODY>
    <s:form action="saveSupplierOrder">
    <div class="subnav" align="left">
           <table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="100%" align="left" height="31">&nbsp;<img src="common/images/title-pk.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt1">采购管理</span>
			&nbsp;<img src="common/images/leftico2.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt2">新建采购单</span></td>
		  </tr>
		</table>
      </div>
     <div class="main_1">
       <table width="99%" border="0" cellpadding="5" cellspacing="1" bgcolor="#D3DEEF">
         <tr>
           <td align="left" class="Blue14" bgcolor="#FAFAFA" colspan="2">采购单信息</td>
         </tr>
		<tr>
	      <td bgcolor="#FAFAFA" align="right"><strong>供应商名称</strong></td>
	      <td bgcolor="#FAFAFA">
			<s:textfield id="order_supplierName" name="order.supplierName" size="30" cssClass="{required:true,maxlength:60}"/>
			<s:hidden id="order_supplierId" name="order.supplierId" />
			<span class="red-text1">*</span>
		  </td>
		</tr>
		<tr>
	      <td bgcolor="#FAFAFA" align="right"><strong>对方合同号</strong></td>
	      <td bgcolor="#FAFAFA">
		    <s:textfield name="order.contractNo" size="20" cssClass="{maxlength:20}"/>
	      </td>
		</tr>
         <tr>
		    <td width="19%" bgcolor="#FAFAFA" align="right"><strong>下单日期</strong></td>
		    <td width="81%" bgcolor="#FAFAFA">
		      <s:textfield name="order.createDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="true" size="10" cssClass="{required:true}"/>
		      <span class="red-text1">*</span>
		    </td>
		 </tr>
         <tr>
		    <td width="19%" bgcolor="#FAFAFA" align="right"><strong>要求到货日期</strong></td>
		    <td width="81%" bgcolor="#FAFAFA">
		      <s:textfield name="order.exceptArriveDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="true" size="10" cssClass="{required:true}"/>
		      <span class="red-text1">*</span>
		    </td>
		 </tr>
         <tr>
		    <td bgcolor="#FAFAFA" align="right"><strong>总金额</strong></td>
		    <td bgcolor="#FAFAFA">
				<s:textfield id="order_totleAmount" name="order.totleAmount" readonly="true" value="0" size="10" cssClass="inputdisabled"/>
			</td>
		 </tr>
       </table>
     </div>
     <div class="clear"></div>
	 <div class="main_scroll">
       <table width="1300px" cellpadding="0" cellspacing="1" border="0" class="ksglxx2a" id="materialsList">
        <tr class="ksglxx2bg">
          <th width="60px" >&nbsp;</th>
          <th width="50px" >大分类</th>
          <th width="60px" >中分类</th>
          <th width="95px" >名称</th>
          <th width="180px" >型号</th>
          <th width="180px" >其他</th>
		  <th width="50px">单位</th>
		  <th width="60px">库存数</th>
		  <th width="95px">生产批号</th>
		  <th width="65px">采购数量<span class="red-text1">*</span></th>
		  <th width="65px">价格<span class="red-text1">*</span></th>
		  <th width="95px">金额</th>
		  <th width="125px" align="left">备注</th>
		  <th >&nbsp;</th>
		</tr>
        <tr class="odd">
          <td><input type="button" value="请选择" onclick="openSelectDialog(0)" />
		       <s:hidden id="materialsNos-0" name="materialsNos" />
          </td>
          <td><span id="materialsRootCategorys-0"></span></td>
          <td><span id="materialsCategorys-0"></span></td>
          <td><span id="materialsNames-0"></span></td>
          <td><span id="materialsTypes-0"></span></td>
          <td><span id="materialsSkuNameSpans-0"></span>
    		  <s:hidden id="materialsIds-0" name="materialsIds" />
              <s:hidden id="materialsSkuNames-0" name="materialsSkuNames" /></td>
          <td><span id="materialsUnits-0"></span></td>
          <td><span id="storeNumbers-0"></span></td>
          <td><s:textfield id="planNos-0" name="planNos" size="10" cssClass="{maxlength:10}" /></td>
          <td>
    		<s:textfield id="materialsNums-0" name="materialsNums" size="5" 
					cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount(0)"/></td>
		  <td><s:textfield id="materialsPrices-0" name="materialsPrices" size="5" 
					cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount(0)"/></td>
		  <td><s:textfield id="materialsAmounts-0" name="materialsAmounts" readonly="true" size="10" value="0" cssClass="inputdisabled"/></td>
          <td><s:textfield id="remarks-0" name="remarks" size="15" cssClass="{maxlength:30}" /></td>
          <td></td>
	    </tr>
        <tr>
         <td colspan="3" align="left" valign="top">
           <input type="button" value="添加" class="input1" onclick="addLine()" />
         </td>
        </tr>
       </table>
      </div>
     <div class="clear"></div>
	 <div class="main_1">
		<table width="98%" border="0" cellpadding="5" cellspacing="0" class="gr-text1">
		   <tr>
			 <td width="45%" align="right">
			   <s:submit value="保存" cssClass="input1" />&nbsp;&nbsp;
			   <input type="button" value="关闭" class="input1" onclick='javascript:window.location.href="supplierOrderList.action"' /></td>
			 <td width="55%" align="left"></td>
		   </tr>
	   </table>
	</div>
	<div class="clear"></div>
	</s:form>
</BODY></HTML>
